package java_thinking.unit_11._11_12;

import java_thinking.TypeInfo.pets.Pet;
import java_thinking.TypeInfo.pets.Pets;

import java.util.Iterator;

/**
 * @Description : CollectionSequence之后的升级
 *      如果类以及继承了其他类，那么你就不能再继承AbstractCollection，在这种情况下，要实现Collection
 *      就必须实现该接口中的所有方法，此时，继承并提供创建迭代器的能力就会显得容易很多
 * @author: cww
 * @DateTime: 2019-03-18 23:58
 */

class PetSequence{
    protected Pet[] pets = Pets.createArray(8);
}

public class NonCollectionSequence extends PetSequence{
    public Iterator<Pet> iterator(){
        return new Iterator<Pet>() {
            private int index = 0;
            @Override
            public void remove() {
                throw new UnsupportedOperationException("remove");
            }

            @Override
            public boolean hasNext() {
                return index<pets.length;
            }

            @Override
            public Pet next() {
                return pets[index++];
            }
        };
    }

    public static void main(String[] args) {
        NonCollectionSequence nonCollectionSequence = new NonCollectionSequence();
        InterfaceVsIterator.display(nonCollectionSequence.iterator());
    }
}
